<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var findMode = function (root) {
        let inArray = []
        function inOrder(root) {
          if (!root) return null
          inOrder(root.left)
          inArray.push(root.val)
          inOrder(root.right)
        }
        inOrder(root)
        let map = new Map()
        for (let i = 0; i < inArray.length; i++) {
          if (map.has(inArray[i])) {
            map.set(inArray[i], map.get(inArray[i]) + 1)
          } else {
            map.set(inArray[i], 1)
          }
        }
        //判断是否有重复的，最后返回一个数组
        let res = []
        let maxCount = 0
        for (let [key, value] of map) {
          if (value > maxCount) {
            res = []
            maxCount = value
            res.push(key)
          } else if (value == maxCount) {
            res.push(key)
          }
        }
        return res
      }
    </script>
  </body>
</html>
